using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for CAccessOperator
/// </summary>
/// 
namespace AccessOperator
{
    public class CAccessOperator
    {
        private OleDbConnection CN;
        public CAccessOperator(string strDbPath)
        {
            CN = new OleDbConnection();
            string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += @"Data Source=" + strDbPath;
            CN.ConnectionString = strConnection;
            CN.Open();
            //
            // TODO: Add constructor logic here
            //
        }
        public void OpenDb()
        {
            if(CN.State.ToString()!="Open")
                CN.Open();
        }
        public void CloseDb()
        {
            CN.Close();
            CN.Dispose();
        }
        public OleDbDataReader DbRead(string strSql)
        {
            try
            {

                OleDbCommand cmd = new OleDbCommand(strSql, CN);
                //cmd.Connection = CN;
                //cmd.CommandText = strSql;
                //OpenDb();
                OleDbDataReader dr;
                dr = cmd.ExecuteReader();
                //cmd.Dispose();//free the  Object which has been used 
                return dr;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public bool DbExecuteSql(string strSql)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = CN;
                cmd.CommandText = strSql;
                cmd.ExecuteNonQuery();
                cmd.Dispose();//free the object
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                if (CN.State == System.Data.ConnectionState.Open)
                    CN.Close();
            }
            return true;
        }
    }
}